package com.charity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.charity.domain.CharityBox;
import com.charity.service.ICharityBoxService;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/charity-box")
public class CharityBoxController {

    @Resource
    private ICharityBoxService charityBoxService;

    @PostMapping("/create")
    public ResponseEntity<Map<String, Object>> createCharityBox(@RequestBody CharityBox charityBox) {
        Map<String, Object> response = new HashMap<>();
        if (charityBoxService.createCharityBox(charityBox)) {
            response.put("code", 200);
            response.put("message", "创建成功");
        } else {
            response.put("code", 400);
            response.put("message", "创建失败");
        }
        return ResponseEntity.ok(response);
    }

    @PutMapping("/update")
    public ResponseEntity<Map<String, Object>> updateCharityBox(@RequestBody CharityBox charityBox) {
        Map<String, Object> response = new HashMap<>();
        if (charityBoxService.updateCharityBox(charityBox)) {
            response.put("code", 200);
            response.put("message", "更新成功");
        } else {
            response.put("code", 400);
            response.put("message", "更新失败");
        }
        return ResponseEntity.ok(response);
    }

    @DeleteMapping("/delete/{boxId}")
    public ResponseEntity<Map<String, Object>> deleteCharityBox(@PathVariable Long boxId) {
        Map<String, Object> response = new HashMap<>();
        if (charityBoxService.deleteCharityBox(boxId)) {
            response.put("code", 200);
            response.put("message", "删除成功");
        } else {
            response.put("code", 400);
            response.put("message", "删除失败");
        }
        return ResponseEntity.ok(response);
    }

    @GetMapping("/list")
    public ResponseEntity<Map<String, Object>> getCharityBoxList(
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Map<String, Object> response = new HashMap<>();
        Page<CharityBox> boxes = charityBoxService.getCharityBoxList(status, pageNum, pageSize);
        response.put("code", 200);
        response.put("message", "获取成功");
        response.put("data", boxes);
        return ResponseEntity.ok(response);
    }

}